<!-- 安全库存 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
      <el-form-item label="产品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库分类" prop="warehouseId">
        <el-select v-model="queryParams.warehouseId" placeholder="请选择仓库分类" clearable>
          <el-option
            v-for="dict in warehouseOptions"
            :key="dict.warehouseId"
            :label="dict.warehouseName"
            :value="dict.warehouseId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="">
          <el-checkbox v-model="queryParams.showLtSafe" >显示低于安全存量的产品</el-checkbox>
          <!-- <el-checkbox v-model="queryParams.showGtMax" >显示库存数超过最高存量的产品</el-checkbox> -->
          <el-checkbox v-model="queryParams.showZero" >显示零库存的产品</el-checkbox>
          <!-- <el-checkbox v-model="queryParams.onlyEnable" >不考虑已停用产品</el-checkbox> -->
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="24" style="margin-bottom: 20px;">
        <!-- <el-button type="warning" icon="el-icon-download" size="mini" @click="handleUpload">导入</el-button> -->
      </el-col>
    </el-row>


    <vxe-table
      border
      stripe
      ref="tableRef"
      size="small"
      header-align="center"
      :row-config="rowConfig"
      :column-config="columnConfig"
      @checkbox-change="checkboxChangeEvent"
      @checkbox-all="checkboxAll"
      :data="tableData"
    >
      <!-- <vxe-column type="checkbox" width="40" fixed="left"></vxe-column> -->
      <vxe-column type="seq" width="80" drag-sort></vxe-column>
      <vxe-column field="itemTypeName" show-overflow="title" align="center" title="分类"></vxe-column>
      <vxe-column field="itemCode" width="100" show-overflow="title" align="center" title="产品代码"></vxe-column>
      <vxe-column field="itemName" width="100" show-overflow="title" align="center" title="产品名称"></vxe-column>
      <vxe-column field="specification" show-overflow="title" align="center" title="规格" ></vxe-column>
      <vxe-column field="unitOfMeasure" show-overflow="title" align="center" title="单位名称"></vxe-column>
      <vxe-column field="vendorCode" show-overflow="title" align="center" title="供应商代码"></vxe-column>
      <vxe-column field="vendorName" show-overflow="title" align="center" title="供应商名称"></vxe-column>
      <!-- <vxe-column field="objName" show-overflow="title" align="center" title="采购员代码" ></vxe-column> -->
      <!-- <vxe-column field="warehouseName" show-overflow="title" align="center" title="采购员"></vxe-column> -->
      <!-- <vxe-column field="quantity" width="100" show-overflow="title" align="center" title="生产单位代码"></vxe-column> -->
      <!-- <vxe-column field="age" width="100" show-overflow="title" align="center" title="生产单位"></vxe-column> -->
      <vxe-column field="quantityOnhand" show-overflow="title" align="center" title="当前存量" ></vxe-column>
      <vxe-column field="minStock" show-overflow="title" align="center" title="安全存量"></vxe-column>
      <vxe-column field="maxStock"  show-overflow="title" align="center" title="最高存量"></vxe-column>
      <vxe-column field="auxiliaryQuantity"  show-overflow="title" align="center" title="现有辅助数量"></vxe-column>
      <vxe-column field="remark"  show-overflow="title" align="center" title="备注"></vxe-column>
      <!-- <vxe-column show-overflow="title" align="center" title="操作" fixed="right" width="200">
        <template #default="{ row }">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
            v-if="row.quantityOnhand<row.safeStock"
            >转采购计划</el-button>
        </template>
      </vxe-column> -->
    </vxe-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 安全库存导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的设备数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <!-- <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link> -->
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSafeStock } from "@/api/mes/purchase/safetyStock";
import { listWarehouse } from "@/api/mes/wm/warehouse";
import { getToken } from "@/utils/auth";
export default {
  name: 'SafetyStock',
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemName: null,
        warehouseId: null,
        showLtSafe: false,
        showGtMax: false,
        showZero: false,
        onlyEnable: false,
      },
      tableData: [],
      total: 0,
      columnConfig: {
        drag: true,
        resizable: true,
        storage: true,
        sortConfig: {
          trigger: 'cell',
          remote: true
        },
        align: 'center',
        headerAlign: 'center'
      },
      rowConfig: {
        drag: true,
        keyField: 'id',
        isHover: true,
      },
      warehouseOptions: [],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/mes/wm/outsourcerecpt/importData"
      },
    }
  },
  created() {
    this.getList()
    listWarehouse({ pageNum: 1, pageSize: 9999, withXBK:true }).then(response => {
      this.warehouseOptions = response.rows;
    })
  },
  methods: {
    getList(){
      this.tableData = []
      listSafeStock(this.queryParams).then(res => {
        this.tableData = res.rows
        this.total = res.total
        // console.log(res);
      })
    },
    handleQuery(){
      this.queryParams.pageNum = 1
      this.getList()
      // console.log('handleQuery');
    },
    resetQuery(){
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        itemName: null,
        warehouseId: null,
        showLtSafe: false,
        showGtMax: false,
        showZero: false,
        onlyEnable: false,
      }
      this.getList()
      // console.log('resetQuery');
    },
    handleImport(){
      console.log('handleImport');
    },
    checkboxChangeEvent(checked){
      // this.ids = checked.records.map(item => item.rtId)
      // console.log(checked.records, this.ids);
    },
    checkboxAll(checked){
      // this.ids = checked.records.map(item => item.rtId)
      // console.log(checked.records, this.ids)
    },
    handleUpdate(row){
      // this.$router.push({ path: '/mes/purchase/safetyStock/edit', query: { id: row.id } })
      console.log(row);
    },
    handleUpload(){
      return this.$message.warning('暂时没有接口');
      this.upload.title = "安全库存数据导入";
      this.upload.open = true;
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
  }
}
</script>

<style lang="scss" scoped>

</style>